package org.apache.ibatis.ibator.generator.ibatis3.pagination.xmlmapper;

import org.apache.ibatis.ibator.api.dom.xml.Attribute;
import org.apache.ibatis.ibator.api.dom.xml.TextElement;
import org.apache.ibatis.ibator.api.dom.xml.XmlElement;
import org.apache.ibatis.ibator.generator.ibatis3.xmlmapper.elements.AbstractXmlElementGenerator;
import org.apache.ibatis.ibator.internal.util.StringUtility;

/**
 * 非BLOB字段分页查询
 * 
 * @author liwb 2016年1月27日 下午7:42:16
 */
public class SelectByExampleWithoutBLOBsPaginationtGenerator extends
        AbstractXmlElementGenerator {

    public SelectByExampleWithoutBLOBsPaginationtGenerator() {
        super();
    }

    @Override
    public void addElements(XmlElement parentElement) {

        XmlElement answer = new XmlElement("select"); //$NON-NLS-1$

        answer.addAttribute(new Attribute("id", //$NON-NLS-1$
                introspectedTable.getSelectByExampleStatementId()
                        + "Pagination"));
        answer.addAttribute(new Attribute(
                "resultMap", introspectedTable.getBaseResultMapId())); //$NON-NLS-1$
        answer.addAttribute(new Attribute("parameterType", "hashmap")); //$NON-NLS-1$

        ibatorContext.getCommentGenerator().addComment(answer);

        answer.addElement(new TextElement("select")); //$NON-NLS-1$
        XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
        ifElement.addAttribute(new Attribute("test", "distinct")); //$NON-NLS-1$ //$NON-NLS-2$
        ifElement.addElement(new TextElement("distinct")); //$NON-NLS-1$
        answer.addElement(ifElement);

        StringBuilder sb = new StringBuilder();
        if (StringUtility.stringHasValue(introspectedTable
                .getSelectByExampleQueryId())) {
            sb.append('\'');
            sb.append(introspectedTable.getSelectByExampleQueryId());
            sb.append("' as QUERYID,"); //$NON-NLS-1$
            answer.addElement(new TextElement(sb.toString()));
        }
        answer.addElement(getBaseColumnListElement());

        sb.setLength(0);
        sb.append("from "); //$NON-NLS-1$
        sb.append(introspectedTable
                .getAliasedFullyQualifiedTableNameAtRuntime());
        answer.addElement((new TextElement(sb.toString())));
        // answer.addElement(getExampleIncludeElement());

        XmlElement includeElement = new XmlElement("include");
        includeElement.addAttribute(new Attribute("refid", introspectedTable
                .getExampleWhereClauseId() + "_Pagination"));
        answer.addElement(includeElement);

        //ifElement = new XmlElement("if"); //$NON-NLS-1$
        //ifElement.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
        //ifElement.addElement(new TextElement("order by ${orderByClause}")); //$NON-NLS-1$
        // answer.addElement(ifElement);

        if (ibatorContext.getPlugins()
                .sqlMapSelectByExampleWithoutBLOBsElementGenerated(answer,
                        introspectedTable)) {
            parentElement.addElement(answer);
        }
    }

}
